<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :addPer="'brandLargeCla:create'"
        :selectPer="'brandLargeCla:page'"
        addTitle="新增类别"
        @insert="add"
        @search="search"
        :showSearchButton="true"
        :showAddButton="true"
      >
        <el-select
          clearable
          style="width: 200px"
          v-model="form.upCategoryId"
          placeholder="上级类别"
        >
          <el-option
            v-for="item in getOneCat"
            :key="item.index"
            :value="item.id"
            :label="item.largeName"
          />
        </el-select>
        <el-select
          clearable
          style="width: 200px"
          v-model="form.level"
          placeholder="类别等级"
        >
          <el-option
            v-for="item in getDict(['类别等级'])['类别等级']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>

        <el-input
          v-model="form.largeName"
          clearable
          placeholder="类别名称"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; margin-left: 20px; cursor: pointer"
            v-permission="'brandLargeCla:delete'"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
      <el-dialog
        title="新增/修改类别"
        :visible.sync="visible"
        width="30%">
        <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
          <el-row>
            <el-col :span="24">
              <el-form-item label="所属门店:" prop="storeId">
                <el-select
                  class="w-100"
                  clearable
                  v-model="addForm.storeId"
                  placeholder="所属门店"
                >
                  <el-option
                    v-for="item in stores"
                    :key="item.id"
                    :value="item.id"
                    :label="item.storeName"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="类别名称:" prop="largeName">
                <el-input v-model="addForm.largeName" clearable placeholder="请输入类别名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="上级类别:" prop="upCategoryId">
                <el-select
                  class="w-100"
                  clearable
                  v-model="addForm.upCategoryId"
                  placeholder="上级类别"
                >
                  <el-option
                    v-for="item in getOneCat"
                    :key="item.index"
                    :value="item.id"
                    :label="item.largeName"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
         <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleOk">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import {
  getList,
  save,
  getOneCat,
  deleteById,
  update
} from '@/api/pre-sale/qualityManagement/category'
import { getDict } from '@/utils/index'
import { mapGetters } from 'vuex'

export default {
  mixins: [
    baseMixin({
      idName: 'id',
      findData: getList,
      updateData: update,
      deleteData: deleteById,
      dataName: ['content', 'totalRecords'],
      format(res) {
        return res.map((data) => {
          data.level = data.level === 1 ? '一级' : '二级'
          return data
        })
      }
    })
  ],
  computed: {
    ...mapGetters(['stores', 'storesCalue'])
  },
  data() {
    return {
      getOneCat: [],
      addForm: {},
      addFormRules: {},
      visible: false,
      getDict: getDict,
      form: {
        client: '' // 客户信息
      },
      tableColumnAttributes: [
        {
          name: '归属门店',
          value: 'storeName',
          type: 'text'
        },
        {
          name: '类别等级',
          value: 'level',
          type: 'text'
        },
        {
          name: '类别名称',
          value: 'largeName',
          type: 'text'
        },
        {
          name: '上级类别',
          value: 'pcategoryName',
          type: 'text'
        },
        {
          name: '上次修改时间',
          value: 'updateTime',
          type: 'text'
        }
      ],
      dict: {}
    }
  },
  methods: {
    // 查询
    search(form) {
      this.searchOptions = form
      this.currentPage = 1
      this.setTableData()
    },
    // 新增
    add() {
      this.getOneCatList()
      this.visible = true
    },
    // 取消
    handleClose() {
      this.$refs.addForm.resetFields()
      this.visible = false
    },
    // 确认
    handleOk() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.save()
        }
      })
    },
    async save() {
      await save({ ...this.addForm }).then((res) => {
        const { success } = res
        if (success) {
          this.search()
          this.visible = false
          this.$refs.addForm.resetFields()
        }
      })
    },
    async getOneCatList() {
      await getOneCat(this.$route.params.id).then((res) => {
        const { success, data } = res
        if (success) {
          this.getOneCat = data
        }
      })
    }

  },
  async mounted() {
    this.getOneCatList()
  }
}
</script>
